# this version of getCSPPattern represents a experimental version
# of pattern detection

getCSPPattern1 <- function(TS, from="1992", to=NULL) {
  if (!is.xts(TS)) {
    stop("TS argument supplied to getCSPPattern is not a xts object")
  }
  Hammer <- CSPHammer(TS) & TrendDetectionChannel(lag(TS,k=1))[,"DownTrend"]
  HangingMan <- CSPHammer(TS) & TrendDetectionChannel(lag(TS,k=1))[,"UpTrend"]
  colnames(HangingMan) <- c("HangingMan")
  InvHammer <- CSPInvertedHammer(TS) & TrendDetectionChannel(lag(TS,k=1))[,"DownTrend"]
  ShootingStar <- CSPInvertedHammer(TS) & TrendDetectionChannel(lag(TS,k=1))[,"UpTrend"]
  colnames(ShootingStar) <- c("ShootingStar")
  Doji <- CSPDoji(TS)
  # calculate ATR for Doji
  ATRlength <- ATR(cbind(Hi(TS), Lo(TS), Cl(TS)), n=20, maType="SMA")[,"atr"]
  BullDoji <- Doji[,"Doji"] & TrendDetectionChannel(lag(TS,k=1))[,"DownTrend"] &
    CandleLength(TS)[,"absCandleLength"] > 0.8*ATRlength
  colnames(BullDoji) <-c("Bull.Doji")
  BearDoji <- Doji[,"Doji"] & TrendDetectionChannel(lag(TS,k=1))[,"UpTrend"] &
    CandleLength(TS)[,"absCandleLength"] > 0.8*ATRlength
  colnames(BearDoji) <-c("Bear.Doji")
  LongDFDoji <- Doji[,"DragonflyDoji"] & 
    TrendDetectionChannel(lag(TS,k=1))[,"DownTrend"] &
    CandleLength(TS)[,"absCandleLength"] > 0.8*ATRlength
  LongGSDoji <- Doji[,"GravestoneDoji"] & 
    TrendDetectionChannel(lag(TS,k=1))[,"UpTrend"] &
    CandleLength(TS)[,"absCandleLength"] > 0.8*ATRlength
  Marubozu <- CSPMarubozu(TS)
  GAP <- CSPGap(TS)
  GapUp <- GAP[,"GapUp"] & TrendDetectionChannel(lag(TS, k=2))[,"UpTrend"]
  GapDown <- GAP[,"GapDown"] & TrendDetectionChannel(lag(TS, k=2))[,"DownTrend"]
  TasukiGap <- CSPTasukiGap(TS)
  UpTG <- TasukiGap[,"UpsideTasukiGap"] & TrendDetectionChannel(lag(TS,k=3))[,"UpTrend"]
  DownTG <- TasukiGap[,"DownsideTasukiGap"] & TrendDetectionChannel(lag(TS,k=3))[,"DownTrend"]
  Engulfing <- CSPEngulfing(TS)
  BullEngulfing <- Engulfing[,"Bull.Engulfing"] & TrendDetectionChannel(lag(TS,k=2))[,"DownTrend"]
  BearEngulfing <- Engulfing[,"Bear.Engulfing"] & TrendDetectionChannel(lag(TS,k=2))[,"UpTrend"]
  PiercingPattern <- CSPPiercingPattern(TS) & TrendDetectionChannel(lag(TS,k=2))[,"DownTrend"]
  DarkCloudCover <- CSPDarkCloudCover(TS) & TrendDetectionChannel(lag(TS,k=2))[,"UpTrend"]
  Harami <- CSPHarami(TS)
  BullHarami <- Harami[,"Bull.Harami"] & TrendDetectionChannel(lag(TS,k=2))[,"DownTrend"]
  BearHarami <- Harami[,"Bear.Harami"] & TrendDetectionChannel(lag(TS,k=2))[,"UpTrend"]
  Star <- CSPStar(TS)
  MorningStar <- Star[,"MorningStar"] & TrendDetectionChannel(lag(TS,k=3))[,"DownTrend"]
  EveningStar <- Star[,"EveningStar"] & TrendDetectionChannel(lag(TS,k=3))[,"UpTrend"]
  ThreeWhiteSoldiers <- CSPThreeWhiteSoldiers(TS) & TrendDetectionChannel(lag(TS,k=3))[,"DownTrend"]
  ThreeBlackCrows <- CSPThreeBlackCrows(TS) & TrendDetectionChannel(lag(TS,k=3))[,"UpTrend"]
  NonStrictTWS <- CSPThreeWhiteSoldiers(TS, strict=FALSE) & TrendDetectionChannel(lag(TS,k=3))[,"DownTrend"]
  colnames(NonStrictTWS) <- c("NonStrictTWS")
  NonStrictTBC <- CSPThreeBlackCrows(TS, strict=FALSE) & TrendDetectionChannel(lag(TS,k=3))[,"UpTrend"]
  colnames(NonStrictTBC) <- c("NonStrictTBC")
  

  if (is.null(to)) {
    subsetstring = paste(from, "/", sep="")
  } else {
    subsetstring = paste(from, to, sep="/")
  }
  result <- cbind(
    Hammer,
    HangingMan,
    InvHammer,
    ShootingStar,
    BullDoji,
    BearDoji,
    LongDFDoji,
    LongGSDoji,
    Marubozu[,"WhiteMarubozu"],
    Marubozu[,"BlackMarubozu"],
    GapUp,
    GapDown,
    UpTG,
    DownTG,   
    BullEngulfing,
    BearEngulfing,
    PiercingPattern,
    DarkCloudCover,
    ATStomach,
    BTStomach,
    BullHarami,
    BearHarami,
    MorningStar,
    EveningStar,
    ThreeWhiteSoldiers,
    ThreeBlackCrows,
    NonStrictTWS,
    NonStrictTBC
    )
  result$random <- FALSE
  # random 5% of bars
  result$random[sample(1:nrow(result),round(nrow(result)/20),replace=FALSE),]<-TRUE
  return (result[subsetstring])
}
